// pages/createAddress/createAddress.js
const app = getApp();
import api from '../../api/index.js';
const { request } = require('../../utils/wx-api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressId:'',
    parentId:1,
    provinces:'',
    province:'省份',
    provinceId:'',
    citys:'',
    city:'城市',
    cityId:'',
    countys:'',
    county:'区县',
    countyId:'',
    value: [0, 0, 0],
    pickX:100,
    name:'',
    detailArea:'',
    tel:'',
    isDefault:false
  },
  bindChange(e){
    // console.log(e);
    let val = e.detail.value
    let provinceId = this.data.provinces[val[0]].id;
    // console.log('provinceId',provinceId)
    request({ url: api.regionList }, { parentId: provinceId })
    .then(res => {
      let citys = res.data.data;
      let city = citys[val[1]].name;
      this.setData({
        province: this.data.provinces[val[0]].name,
        provinceId,
        citys,
        city
      })
      let cityId = this.data.citys[val[1]].id;
      // console.log('cityId',cityId);
      request({ url: api.regionList }, { parentId: cityId })
      .then(res=>{
        let countys = res.data.data;
        let county = countys[val[2]].name;
        this.setData({
          countys,
          county,
          cityId
        })
        let countyId = this.data.countys[val[2]].id;
        // console.log('countyId', countyId);
        this.setData({
          countyId
        })
      })
    })
    // console.log("provinceId", provinceId);
  },
  showPick(){
    this.setData({
      pickX:0
    })
  },
  seleSure(){
    this.setData({
      pickX:100
    })
  },
  isDefault(){  //是否选为默认地址
    let {isDefault} = this.data;
    this.setData({
      isDefault: !isDefault
    })
  },
  save(){ //保存地址
    let {name} = this.data;
    let {detailArea} = this.data;
    let {tel} = this.data;
    request({url:api.addressSave},{
      id: this.data.addressId,
      name,
      mobile:tel,
      province_id: this.data.provinceId,
      city_id:this.data.cityId,
      district_id:this.data.countyId,
      address: detailArea,
      is_default: this.data.isDefault
    },'POST',{'X-Nideshop-Token':app.globalData.token})
    .then(res=>{
      console.log('save',res);
      wx.navigateBack({
        delta:1
      })
    })
  },
  bindinput(e){
    let val = e.detail.value;
    let {i} = e.currentTarget.dataset;
    if(i=='0'){
      this.setData({
        detailArea:val
      })
    }else if(i=='1'){
      this.setData({
        name:val
      })
    }else {
      this.setData({
        tel:val
      })
    }
    return val
  },
  cancel(){ //点击取消，返回上一层
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options);
    if(options.id) {
      request({url:api.addressDetail},{id:options.id},'GET',{'X-Nideshop-Token':app.globalData.token})
      .then(res=>{
        console.log("地址详情", res);
        let detail = res.data.data;
        this.setData({
          addressId:detail.id,
          province: detail.province_name,
          provinceId: detail.province_id,
          city: detail.city_name,
          cityId: detail.city_id,
          county: detail.district_name,
          countyId: detail.district_id,
          detailArea: detail.address,
          name: detail.name,
          tel: detail.mobile,
          isDefault: detail.is_default
        })
      })
    }
    request({ url: api.regionList }, { parentId: 1 })
      .then(res => {
        // console.log(res);
        let provinces = res.data.data;
        this.setData({
          provinces
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})